<?php
/// http://192.168.18.23/master/mcGM/tmp_process/recvDeviceactivation.php?hide_type=1&logtype=1&device_id=159753&device_mac=88:32:9B:3F:96:8B&platform_id=uc
/// http://192.168.18.23/master/mcGM/tmp_process/recvDeviceactivation.php?hide_type=2&device_id=159753&param6=283947fj2309

///http://192.168.18.23/master/mcGM/tmp_process/recvDeviceactivationEncode.php?hide_type=%32&device_id=%30%45%37%36%42%41%36%45%2d%46%41%37%42%2d%35%45%46%36%2d%38%36%34%43%2d%34%44%32%46%41%35%33%33%32%31%37%33&param12=%4c%6f%67%69%6e%53%75%63%63%65%73%73%2d%31%34%36%31%33%31%30%31%38%34&account_id=%31%39%34%37&idfa=%45%64%69%74%6f%72%48%61%73%4e%6f%49%64%66%61

#$memcachehost = '192.168.18.23';
#$memcacheport = 11211;
#$memcachelife = 10;

#$KeyLockCache1 = "KeyLockCache1";
#$KeyLockCache2 = "KeyLockCache2";
#$KeyCacheData1 = "KeyCacheData1";
#$KeyCacheData2 = "KeyCacheData2";
#
#$FLAG_UNLOCK = "0";
#$FLAG_LOCK_C = "1";
#$FLAG_LOCK_P = "2";


define("APP_ROOT",dirname(__FILE__) . "/");

include APP_ROOT . "cur_db_cfg.php";

# 解析URL数据
$row = $_GET;

$memkeydata = "";
$row_device_id = $row['device_id'];
if(0 == strlen($row_device_id))
{
    echo("error device_id is empty");
    return ;
}


if("1" == $row["hide_type"])
{
    $row['inserttime'] = date("Y-m-d H:i:s", time()+8*3600);
    $strdata = "insert into deviceactivation(";
    $strdata_laster = "";
    $strdata_val = "";
    
    $flag = 0;
    foreach ($row as $k=>$v)
    { 
        $row_k = $row[$k];
        if( $k == 'hide_type')
        {
        }
        else if(null != isset($row_k))
        {
            if(1 == $flag)
            {
                $strdata = $strdata . ",";
                $strdata_laster = $strdata_laster . ",";
                $strdata_val = $strdata_val . ",";
            }
            $strdata = $strdata . $k;
            if($k == 'logtype' || $k == 'installtime')
            {
                $tmpval = $row_k;
            }
            else
            {
                $tmpval = "'$row_k'";
            }
            $strdata_laster = $strdata_laster . $tmpval;
            $strdata_val = $strdata_val . $k . "=" . $tmpval;
            $flag = 1;
        }
        else
        {
            $flag = 0;
        }
    }
    $strdata = $strdata . ")";
    $strdata = $strdata . "values(" . $strdata_laster . ") on duplicate key update ";
    $strdata = $strdata . $strdata_val;
}
else if("2" == $row["hide_type"])
{
    if(null == isset($row["account_id"]))
    {
        $strdata = stringsplit1($row, $row_device_id, 1, 40);
        if(0 == strlen($strdata))
        {
            $strdata = stringsplit1($row, $row_device_id, 1000, 1007);
        }
    }
    else
    {
        $strdata = stringsplit2($row, $row_device_id, 1, 40);
        if(false == isset($strdata))
        {
            $strdata = stringsplit2($row, $row_device_id, 1000, 1007);
        }
    }
}


function stringsplit1($row, $row_device_id, $loop_b, $loop_e)
{
    $ss_data = "";
    $tmp_channel = $row["channel"];
    $tmp_version = $row["version"];
    for ($loop=$loop_b; $loop<=$loop_e; $loop++)
    {
        $tmpStr = "param" . $loop;
        if(null == isset($row[$tmpStr]))
        {
            continue;
        }
        $row_tmpStr = $row[$tmpStr];
        if(0 != strlen($row_tmpStr))
        {
            $ss_data = "insert into deviceactivation(device_id, channel, version, $tmpStr) values('";
            $ss_data = $ss_data . $row_device_id . "', '";
            $ss_data = $ss_data . $tmp_channel . "', '";
            $ss_data = $ss_data . $tmp_version . "', '";
            $ss_data = $ss_data . $row_tmpStr . "') on duplicate key update ";
            $ss_data = $ss_data . "device_id='" . $row_device_id . "', ";
            $ss_data = $ss_data . "channel='" . $tmp_channel . "', ";
            $ss_data = $ss_data . "version='" . $tmp_version . "', ";
            $ss_data = $ss_data . $tmpStr . "='$row_tmpStr'";
        }
    }

    return $ss_data;
}

function stringsplit2($row, $row_device_id, $loop_b, $loop_e)
{
    $tmp_account = $row["account_id"];
    $tmp_idfa = $row["idfa"];
    $tmp_channel = $row["channel"];
    $tmp_version = $row["version"];
    for ($loop=$loop_b; $loop<=$loop_e; $loop++)
    {
        $tmpStr = "param" . $loop;
        if(null == isset($row[$tmpStr]))
        {
            continue;
        }
        $row_tmpStr = $row[$tmpStr];
        if(0 != strlen($row_tmpStr))
        {
            $ss_data = "insert into deviceactivation(device_id, account_id, idfa, channel, version, $tmpStr) values('";
            $ss_data = $ss_data . $row_device_id . "', '";
            $ss_data = $ss_data . $tmp_account . "', '";
            $ss_data = $ss_data . $tmp_idfa . "', '";
            $ss_data = $ss_data . $tmp_channel . "', '";
            $ss_data = $ss_data . $tmp_version . "', '";
            $ss_data = $ss_data . $row_tmpStr . "') on duplicate key update ";
            $ss_data = $ss_data . "device_id='" . $row_device_id . "', ";
            $ss_data = $ss_data . "account_id='" . $tmp_account . "', ";
            $ss_data = $ss_data . "channel='" . $tmp_channel . "', ";
            $ss_data = $ss_data . "version='" . $tmp_version . "', ";
            $ss_data = $ss_data . "idfa='" . $tmp_idfa . "', ";
            $ss_data = $ss_data . $tmpStr . "='$row_tmpStr'";
        }
    }
    return $ss_data;
}

if(false == isset($strdata))
{
    echo($strdata . "<br>");
	$myfile = fopen("c:/recv.txt", "w") or die("Unable to open file!");
	$txt = $_SERVER['REQUEST_URI'] . "\n";
	fwrite($myfile, $txt);
	fclose($myfile);
	return ;
}

$conn=mysql_connect($dbhost,$dbaccount,$dbpwd);
if(null == $conn)
{
    echo("mysql connect failed " . mysql_error());
    echo("<br>");
    return ;
}
mysql_select_db("shouhuzhemen_log");
$result_db=mysql_query($strdata);
if(true == $result_db)
{
    echo("successful");
}
else
{
    echo($strdata . "<br>");
    echo("failed  " . mysql_error());
}


/// 内存缓冲
#$memcache = new Memcache;
#$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
#
#if(!$memcache->get($row['device_id']))
#{
#    #echo("add device_id<br>");
#    $memcache->add($row['device_id'], $strdata, 0, $memcachelife);
#}
#else
#{
#    $memcache->set($row['device_id'], $strdata, 0, $memcachelife);
#}
#if(!$memcache->get($KeyLockCache1))
#{
#    #echo("add " . $KeyLockCache1. "<br>");
#    $memcache->add($KeyLockCache1, $FLAG_UNLOCK, 0, $memcachelife);
#}
#if(!$memcache->get($KeyLockCache2))
#{
#    #echo("add " . $KeyLockCache2 . "<br>");
#    $memcache->add($KeyLockCache2, $FLAG_UNLOCK, 0, $memcachelife);
#}
#if(!$memcache->get($KeyCacheData1))
#{
#    #echo("add " . $KeyCacheData1 . "<br>");
#    $memcache->add($KeyCacheData1, "", 0, $memcachelife);
#}
#if(!$memcache->get($KeyCacheData2))
#{
#    #echo("add " . $KeyCacheData2 . "<br>");
#    $memcache->add($KeyCacheData2, "", 0, $memcachelife);
#}


#while(true)
#{
#    /// 上锁处理
#    $KeyCacheData = "";
#    if($FLAG_UNLOCK == $memcache->get($KeyLockCache1))
#    {
#        $memcache->set($KeyLockCache1, $FLAG_LOCK_P);
#        $KeyCacheData = $KeyCacheData1;
#        $KeyLockCache = $KeyLockCache1;
#    }
#    else if($FLAG_UNLOCK == $memcache->get($KeyLockCache2))
#    {
#        $memcache->set($KeyLockCache2, $FLAG_LOCK_P);
#        $KeyCacheData = $KeyCacheData2;
#        $KeyLockCache = $KeyLockCache2;
#    }
#    
#    
#    /// 把设备ID加到字符串队列中
#    $tmpCacheData = $memcache->get($KeyCacheData);
#    if(0 == strlen($tmpCacheData))
#    {
#        $tmpCacheData = $row['device_id'];
#    }
#    else
#    {
#        $tmpCacheData = $tmpCacheData . "^" . $row['device_id'];
#    }
#    if($FLAG_LOCK_P == $memcache->get($KeyLockCache))
#    {
#        $memcache->set($KeyCacheData, $tmpCacheData);
#        $memcache->set($KeyLockCache, $FLAG_UNLOCK);
#        
#        echo("successful");
#        return ;
#    }
#}


?>
